#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/uio.h>
#include <sys/times.h>

#define BUFSIZE_1       1280*1000
#define BUFSIZE_2       1024*1000
#define BUFSIZE         BUFSIZE_1+BUFSIZE_2

#define CPS             (double)(sysconf(_SC_CLK_TCK))

int main(int argc, char const* argv[])
{
    struct iovec iov[2];
    struct tms t1, t2;
    clock_t c1, c2;
    char *buf, *buf1, *buf2;

    buf = malloc(BUFSIZE);
    buf1 = malloc(BUFSIZE_1);
    buf2 = malloc(BUFSIZE_2);

    iov[0].iov_base = buf1;
    iov[0].iov_len = BUFSIZE_1;
    iov[1].iov_base = buf2;
    iov[1].iov_len = BUFSIZE_2;

    printf("for writev:\n");

    c1 = times(&t1);

    if (writev(1, iov, 2) < 0)
    {
        perror("writev error");
        exit(0);
    }

    c2  = times(&t2);

    printf("realtime:%7.2f, usertime:%7.2f, systime:%7.2f\n", (c2-c1)/CPS, 
        (t2.tms_utime-t1.tms_utime)/CPS, (t2.tms_stime-t1.tms_stime)/CPS);

    printf("for only-one-write:\n");

    c1 = times(&t1);

    memcpy(buf, buf1, BUFSIZE_1);
    memcpy(buf+BUFSIZE_1, buf2, BUFSIZE_2);
    if (write(1, buf, BUFSIZE) < 0)
    {
        perror("writev error");
        exit(0);
    }

    c2  = times(&t2);

    printf("realtime:%7.2f, usertime:%7.2f, systime:%7.2f\n", (c2-c1)/CPS, 
        (t2.tms_utime-t1.tms_utime)/CPS, (t2.tms_stime-t1.tms_stime)/CPS);

    return 0;
}
